<template>
  <div class="approve">
      <!-- <div v-if="type==='1' || type==='2'" class="docu">
          <div class="resTitle">审批意见</div>
          <div>
              <b v-if="type==='1'">同意</b>
              <b v-if="type==='2'">拒绝</b>
          </div>
      </div> -->
     
      <div class="textArea">
        <textarea type="textarea" v-model="leaveReason" refs="areaInput" :placeholder="placeholder" rows="3" style="" cols="20" class="leaveDesc">
        </textarea>
      </div>

      <!--<div class="docuSelect">
          <uploader :src="'api'" :fileList="fileList"></uploader>
      </div>-->
      <div class="copyCon" v-if="!isHr && type !=='2' ">
          <p class="copeTitle">审批人</p>
          <appro-sel @add="selectApproLeader" :approSelList.sync="approPeoLeader"></appro-sel>
      </div>
      <div class="copyCon">
          <p class="copeTitle">知会人</p>
          <appro-sel @add="selectApproCopy" :approSelList.sync="approSelList"></appro-sel>
      </div>
      <button class="btn" :class="{'submitColor':submitColor}" @click="beforeCheck">确定<template v-if="type==='1'">同意</template><template v-if="type==='2'">驳回</template></button>
      <approve-show :showAppro.sync="approShow" :breadArr.sync="breadArr" :deptName="deptName" @searchAppro="selectApproPeo" @getSelPeo="getSelectPeo" :deptList.sync="deptList" :dataList.sync="approPeoList"></approve-show>
      
  </div>
</template>

<script>
import Vue from 'vue'
import {Toast,MessageBox} from 'mint-ui'

import uploader from '@/components/leave/uploader'
import approSel from '@/components/approve/approSel'
// import approveShow from "@/components/leave/approvePeoShow";
const approveShow = r => require.ensure([], () => r(require("@/components/leave/approvePeoShow")), 'approvePeoShow');
export default {
  name: 'app',
  data() {
    return {
      selected: 'A',
      tableader: true,
      num: 9,
      type:null,
      fileList:[],
      exectionId:null,
      leaveReason:'',
      Act:null,

      submitColor: false,
      isSubmitIng: false,

      approShow:false,
      approSelList:[],
      approPeoList:[],
      companyName:'',
      deptName:'',
      breadArr:[],
      deptList:[],

      approPeoLeader:[],
      typeForSel:null,
      isHr:false,


    };
  },
  computed:{
      placeholder:function(){
          switch(this.type){
              case '1':
                  return '请填写审批意见';
                  break;
              case '2':
                  return '请填写审批意见';
                  break;
              case '3':
                  return '想说点什么呢';
                  break;
              case '4':
                  return '请输入转交备注';
                  break;
              default:
                  return "请输入审批意见";
                  break;
          }
      }
  },
  methods: {
    handleAdd:function(){
        this.approShow = true;
    },



    submitStartState: function() {
      this.isSubmitIng = true;
      this.submitColor = true;
    },

    submitEndState: function() {
      this.isSubmitIng = false;
      this.submitColor = false;
    },

    beforeCheck:function(){

        if (this.type==="2" && !this.leaveReason.trim()) {
            Toast({
              message: '请您输入审批意见',
              position: 'center',
              className:'toastStyle',
              duration: 1000
            });
            return;             
        }
        if (this.approPeoLeader && this.approPeoLeader.length>0) {
            this.NU = this.approPeoLeader[0].name;
        }

        if (!this.isHr && this.type !=='2') {
            MessageBox.confirm(
              `您确定要发送给${this.NU}吗？`,'询问'
            )
            .then(action => {
                this.sendAjax();
            })
            .catch(action => {
        
            });
        } else if (this.isHr && this.type !=='2'){
            MessageBox.confirm(
              `您确定要提交吗？`,'询问'
            )
            .then(action => {
                this.sendAjax();
            })
            .catch(action => {
        
            });          
        } else if (this.type ==='2') {
            MessageBox.confirm(
              `您确定要驳回吗？`,'询问'
            )
            .then(action => {
                this.sendAjax();
            })
            .catch(action => {
        
            });             
        }

    },

    sendAjax () {

      if (this.isSubmitIng) {
          return;
      }
      this.submitStartState();

      let type = this.type === '1'? '同意' : '拒绝';
			var params = {
          idea: type,
          excutionId: this.exectionId,
          comment: this.leaveReason,
          vrNodeId:this.Act || '',
      }
      if (this.type=== '1') {
          if (this.approPeoLeader.length) {
            params.auditorId = '';
            this.approPeoLeader.forEach((ele, index, arr) => {
              if (index === arr.length - 1) {
                params.auditorId += `${arr[index].emplid}`;
              } else {
                params.auditorId += `${arr[index].emplid},`;
              }
            });
          }
      }



			this.Axios
				.Post(this.URL.getexaminationApply.url, params)
				.then((res) => {         
            if(res.data.status == 1) {
                this.saveCopyPeo();
            } else {
                this.submitEndState();
                Toast({
                  message: res.data.message || '审批失败',
                  position: 'center',
                  className:'toastStyle',
                  duration: 1000
                });
            }
				})
		},




    //审批人
    selectApproLeader : function(){
        this.typeForSel = 'leader';
        this.selectApproPeo()
    },

    //

    //知会人
    selectApproCopy : function(){
        this.typeForSel = 'copy';
        this.selectApproPeo()
    },

    //

    //

    selectApproPeo:function(name,type){
        console.log(name,'name')
        if (!type) {
            var params = {
              name:name||'',
              sourceType:'A'
            }
           this.Axios
            .Post(this.URL.getCcList.url,params)
            .then((res)=>{
                this.approShow = true;
                this.breadArr=[];
                this.breadArr.push(res.data.datas.fristDept);
                this.breadArr.push(res.data.datas.checkDept);
                this.deptList = res.data.datas.deptList;
                this.approPeoList = res.data.datas.empList;

            });           
        } else {
            let {setid,deptid} = name
            var params = {
              setid,
              deptid
            }

            this.Axios
                .Post(this.URL.getCcListByDept.url,params)
                .then((res)=>{
                    this.approShow = true;

                    if (type === 'deptSearch') {
                        this.breadArr.push(res.data.datas.checkDept);
                    }
                    this.deptList = res.data.datas.deptList;
                    this.approPeoList = res.data.datas.empList;

                });  

            }

   
    },
    getSelectPeo:function(peo){
        if (this.typeForSel === 'leader') {
            this.approSelFunc(this.approPeoLeader,peo)
        }else {
            this.approSelFunc(this.approSelList,peo)
        }

    },

    approSelFunc:function(val,peo){
        for (var i=0,len=val.length; i<len; i++) {
            if (val[i].emplid === peo.emplid){
                Toast({
                  message: '审批人已重复',
                  position: 'center',
                  className:'toastStyle',
                  duration: 1000
                });
                return;  
            }

                     
        }
        
        val.push(peo);        
    },

    removeAppro:function(index){

        if (this.typeForSel === 'leader') {
            this.approPeoLeader.splice(index,1);
        }else {
            this.approSelList.splice(index,1);
            // this.approSelFunc(this.approSelList,peo)
        }
    },

    //
    saveCopyPeo(){
      var params = {
        toEmplids: "",
        applyCode: this.exectionId,
        actName: "",
        actId: "",
        toIdea: "",
        fromIdea: ""
      };

      if (this.approSelList.length) {
        this.approSelList.forEach((ele, index, arr) => {
          if (index === arr.length - 1) {
            params.toEmplids += `${arr[index].emplid}`;
          } else {
            params.toEmplids += `${arr[index].emplid},`;
          }
        });
      }

      this.Axios
        .Post(this.URL.copyToSend.url, params)
        .then((res) => {

      
          if(res.data.status == 1) {
                Toast({
                  message: '审批成功',
                  position: 'center',
                  className:'toastStyle',
                  duration: 1000
                });
                setTimeout(()=>{
                    this.submitEndState();
                    location.href="/module/approve.html";
                },1500)
          } else {
                this.submitEndState();
                Toast({
                  message: '抄送人抄送失败',
                  position: 'center',
                  className:'toastStyle',
                  duration: 1000
                });
          }
        }).catch(()=>{
              this.submitEndState();
              Toast({
                message: '系统异常，稍等重试',
                position: 'center',
                className:'toastStyle',
                duration: 2000
              });          
        })
    },
    isHrFunc:function(){
      this.Axios
        .Post(this.URL.isHR.url, {})
        .then((res)=>{
            this.isHr = res.data.datas;
        })
    }




  },
  components: {
    'uploader':uploader,
    approSel,
    approveShow,

  },
  watch: {

  },
  created:function(){
      // this.isHrFunc();
  },
  mounted: function() {
      // var arr = [];
      // location.search.split('&').forEach(function(val,index){
      //     arr.push(val.split('=')[1])
      // })

      var para = JSON.parse(localStorage.getItem('approRes'));
      this.type = para.type;
      this.exectionId= para.EX;
      this.Act =  para.AC;
      this.AT = para.AT;
      this.NU =para.NU;

      this.isHr = this.AT.indexOf('VR') > -1 ? false : true;


      // var res = {"status":1,"message":"处理成功","total":null,"datas":{"deptid":"1000358","deplist":{"id":"49731","descr":"厦漳公司","descrshort":"厦漳公司","c_branch_chief":" ","manager_id":" ","setid":"CM008","c_dept_type":"C","c_comp_lvl":"01","c_dept_lvl":" ","dept_seq":"148026311","deptid":"1000358","parent_deptid":"1000312","effdt":"1900-01-01 00:00:00","eff_status":"A","level":1,"isnext":0,"sort":0,"menuUrl":null,"menuIconCls":"fa-circle-o","personnel":0,"pay":0,"count":0,"childList":null},"list":[{"emplid":"00214759","name":"饶树","deptid":"1000358"},{"emplid":"00214666","name":"魏明其","deptid":"1000358"},{"emplid":"00214667","name":"吴增","deptid":"1000358"},{"emplid":"00214689","name":"谢奕鹏","deptid":"1000358"},{"emplid":"00214686","name":"马圣","deptid":"1000358"},{"emplid":"00214688","name":"邓磊超","deptid":"1000358"},{"emplid":"00214671","name":"高皇强","deptid":"1000358"},{"emplid":"00214672","name":"傅政喜","deptid":"1000358"},{"emplid":"00214673","name":"曾东刚","deptid":"1000358"},{"emplid":"00214674","name":"毛仁文","deptid":"1000358"},{"emplid":"00214681","name":"赵景龙","deptid":"1000358"},{"emplid":"00214682","name":"戚江健","deptid":"1000358"},{"emplid":"00214675","name":"张本轰","deptid":"1000358"},{"emplid":"00214677","name":"郑昭球","deptid":"1000358"},{"emplid":"00214678","name":"刘万军","deptid":"1000358"},{"emplid":"00214654","name":"孔保校","deptid":"1000358"},{"emplid":"00214655","name":"张智金","deptid":"1000358"},{"emplid":"00214656","name":"杨德瑞","deptid":"1000358"},{"emplid":"00214657","name":"赵周权","deptid":"1000358"},{"emplid":"00214658","name":"林景胜","deptid":"1000358"},{"emplid":"00214659","name":"张才伟","deptid":"1000358"},{"emplid":"00214660","name":"辛伟","deptid":"1000358"},{"emplid":"00214661","name":"陈光文","deptid":"1000358"},{"emplid":"00214663","name":"杨浩远","deptid":"1000358"},{"emplid":"00214731","name":"胡涛军","deptid":"1000358"},{"emplid":"00214732","name":"闵珠华","deptid":"1000358"},{"emplid":"00214668","name":"尤科荣","deptid":"1000358"},{"emplid":"00214683","name":"王旭生","deptid":"1000358"},{"emplid":"00214680","name":"宋胜涛","deptid":"1000358"},{"emplid":"00214651","name":"汪志","deptid":"1000358"},{"emplid":"00214652","name":"邵文军","deptid":"1000358"},{"emplid":"00214669","name":"罗伟生","deptid":"1000358"},{"emplid":"00214670","name":"廖孝宇","deptid":"1000358"},{"emplid":"00214691","name":"彭振川","deptid":"1000358"},{"emplid":"00214694","name":"刘博勋","deptid":"1000358"},{"emplid":"00214695","name":"侯晓燕","deptid":"1000358"},{"emplid":"00214696","name":"彭修","deptid":"1000358"},{"emplid":"00214697","name":"梁嘉","deptid":"1000358"},{"emplid":"00214698","name":"黄文疆","deptid":"1000358"},{"emplid":"00214685","name":"赵成生","deptid":"1000358"},{"emplid":"00214716","name":"刘志博","deptid":"1000358"},{"emplid":"00214717","name":"古国","deptid":"1000358"},{"emplid":"00214733","name":"韩学明","deptid":"1000358"},{"emplid":"00214749","name":"邹松涛","deptid":"1000358"},{"emplid":"00214750","name":"黄玮","deptid":"1000358"},{"emplid":"00214751","name":"胡海东","deptid":"1000358"},{"emplid":"00214752","name":"王浪权","deptid":"1000358"},{"emplid":"00214753","name":"彭小军","deptid":"1000358"},{"emplid":"00214754","name":"钟日兴","deptid":"1000358"},{"emplid":"00214755","name":"李进州","deptid":"1000358"},{"emplid":"00214756","name":"冯玉刚","deptid":"1000358"},{"emplid":"00214757","name":"鲁广伟","deptid":"1000358"},{"emplid":"00214758","name":"王义","deptid":"1000358"},{"emplid":"00214704","name":"赵诚","deptid":"1000358"},{"emplid":"00214705","name":"陈辉","deptid":"1000358"},{"emplid":"00214706","name":"陈刚武","deptid":"1000358"},{"emplid":"00214707","name":"陈宁","deptid":"1000358"},{"emplid":"00214708","name":"朱成飞","deptid":"1000358"},{"emplid":"00214710","name":"肖鹏绩","deptid":"1000358"},{"emplid":"00214711","name":"王世荣","deptid":"1000358"},{"emplid":"00214712","name":"李成","deptid":"1000358"},{"emplid":"00214718","name":"张重强","deptid":"1000358"},{"emplid":"00214719","name":"龚波新","deptid":"1000358"},{"emplid":"00214722","name":"张国刚","deptid":"1000358"},{"emplid":"00214723","name":"陈志宙","deptid":"1000358"},{"emplid":"00214724","name":"陈超焰","deptid":"1000358"},{"emplid":"00214725","name":"王世","deptid":"1000358"},{"emplid":"00214726","name":"张金松","deptid":"1000358"},{"emplid":"00214727","name":"彭杰初","deptid":"1000358"},{"emplid":"00214728","name":"詹青光","deptid":"1000358"},{"emplid":"00214714","name":"王骢刚","deptid":"1000358"},{"emplid":"00214734","name":"陈子","deptid":"1000358"},{"emplid":"00214735","name":"宋小平","deptid":"1000358"},{"emplid":"00214736","name":"彭臻承","deptid":"1000358"},{"emplid":"00214701","name":"蔡舒彬","deptid":"1000358"},{"emplid":"00214702","name":"邱子","deptid":"1000358"},{"emplid":"00214737","name":"黄春烨","deptid":"1000358"},{"emplid":"00214738","name":"赵继岩","deptid":"1000358"},{"emplid":"00214739","name":"罗锦","deptid":"1000358"},{"emplid":"00214740","name":"肖成","deptid":"1000358"},{"emplid":"00214741","name":"金涛俊","deptid":"1000358"},{"emplid":"00214743","name":"卫开希","deptid":"1000358"},{"emplid":"00214664","name":"彭小泉","deptid":"1000358"},{"emplid":"00214665","name":"阮日斌","deptid":"1000358"},{"emplid":"00214746","name":"黄伟海","deptid":"1000358"},{"emplid":"00214747","name":"王辉超","deptid":"1000358"},{"emplid":"00214729","name":"肖启","deptid":"1000358"},{"emplid":"00214730","name":"闵新友","deptid":"1000358"},{"emplid":"00214748","name":"张金","deptid":"1000358"},{"emplid":"00214653","name":"吴杨勇","deptid":"1000358"},{"emplid":"00214703","name":"陈海华","deptid":"1000358"},{"emplid":"00214699","name":"邱祥怀","deptid":"1000358"},{"emplid":"00214744","name":"陈晓才","deptid":"1000358"},{"emplid":"00214745","name":"皮学","deptid":"1000358"},{"emplid":"10001006","name":"地产手工练习练习一九","deptid":"1000358"},{"emplid":"00214700","name":"张国清","deptid":"1000358"},{"emplid":"00214679","name":"周日友","deptid":"1000358"},{"emplid":"00214692","name":"徐小","deptid":"1000358"},{"emplid":"00214715","name":"赵园兴","deptid":"1000358"},{"emplid":"00214742","name":"张宏斌","deptid":"1000358"},{"emplid":"00214662","name":"孔峥彪","deptid":"1000358"},{"emplid":"10000898","name":"李白","deptid":"1000358"},{"emplid":"00214687","name":"宁琏奇","deptid":"1000358"},{"emplid":"00214721","name":"赵建伟","deptid":"1000358"},{"emplid":"00214690","name":"王文双","deptid":"1000358"},{"emplid":"00214713","name":"黄呈坐","deptid":"1000358"},{"emplid":"00214720","name":"张华兴","deptid":"1000358"},{"emplid":"00214693","name":"麦云华","deptid":"1000358"},{"emplid":"10001067","name":"张三","deptid":"1000358"},{"emplid":"00214709","name":"李鹏衡","deptid":"1000358"},{"emplid":"00214676","name":"洪伊明","deptid":"1000358"},{"emplid":"00214684","name":"邓伟萍","deptid":"1000358"},{"emplid":"10000949","name":"孙琦龙","deptid":"1000358"},{"emplid":"10000835","name":"洪刚","deptid":"1000358"}]},"page":null,"token":null};
      // this.approPeoList = res.datas.list;
      // document.title = '我爱你';
  }
};
</script>

<style lang="less" scope="scope">
.approve {
  font-family:   "Microsoft YaHei", "微软雅黑";
  font-size: 0.28rem;
  height: 100%;
  position:relative;
  textarea {
    width: 100%;
    resize: none;
    display: inline-block;
    font-size: 0.36rem;
    line-height: 0.6rem;
    height: 0.6rem;
    border: none;
    height: 4rem;
    background-color: transparent;
    border: none; // margin-top:-0.1rem;
    padding: 0 0;
    vertical-align: top;
  }
  textarea::-webkit-input-placeholder {
    color: #aaa;
    font-size:0.28rem;
    line-height: 0.6rem;
    height: 0.6rem;
    font-weight: normal;
    text-align: left;
  }
  textarea:focus {
    outline: none;
  }
  .docu{
    display:flex;
    background-color:#fff;
    align-items:center;
    font-size:0.32rem;
    line-height:0.8rem;

    .resTitle{
      font-size:0.28rem;
      color:#999;
      margin-left:0.3rem;
      margin-right:0.2rem;
    }
    .fileTitle{
      margin-left:0.3rem;
    }
    .fileInput{
        width:80%;
        text-align:right;
    }
  }
  .docuSelect{
    font-size:0.32rem;
    position:relative;
    background-color:#fff;
    overflow: hidden;
    clear: both;
    line-height:0.8rem;
    margin-bottom:0.3rem;
  }
  .copyCon{
    background-color:#fff;
    padding-bottom:0.2rem;
    margin-bottom: 0.3rem;
    >p.copeTitle{
      font-size:0.32rem;
      line-height:1rem;
      padding-left:0.3rem;
    }
  }
  .textArea{
    background-color:#fff;
    margin-bottom:0.3rem;
    padding-left: 0.3rem;
    padding-top:0.3rem;
  }
  .btn {
    width: 80%;
    height: 1rem;
    border: none;
    margin: 0.5rem 10%;
    outline: none;
    font-size: 0.32rem;
    font-weight: 600;
    color: #fff;
    border-radius: 0.5rem;
    background-color: #007de4;
  }
  button.submitColor {
    background-color: #2872af;
  }

}


</style>
<style>
body .mint-msgbox-btns button{
  font-size:0.4rem;
}
</style>


